//
//  BindEmailViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/7.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class BindEmailViewModel: BaseViewModel {
   open var model:SecurityModel? //传递的model
    var emailText:String = ""
    var codeText:String = ""
    var googleText:String = ""
    
    lazy var codeRequest:MobileCodeRequest = {
        let api = MobileCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var emailRequest:BindEmailRequest = {
        let api = BindEmailRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    lazy var emailCodeRequest:EmailCodeRequest = {
        let api = EmailCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    /// 发送验证码
    ///
    ///
    func sendCode(btn: TimerBtn)  {
        btn.isEnabled = false
        self.request(request: codeRequest, success: { (result) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func sendEmailCode(btn: TimerBtn) {
        btn.isEnabled = false
        self.request(request: emailCodeRequest, success: { (result) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func sendInformation() {
        self.request(request: emailRequest, success: {[weak self] (result) in
            CYMBShow.showMessage(kText("邮箱验证成功"))
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
            self?.controller?.navigationController?.popViewController(animated: true)
        }) { (request) in
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func archiveCellKind() {
        var array:[CYTableviewItem] = []
        let itemFir = CYTableviewItem(type: .cyNameWrite, name: kText("邮箱"), placeHolder: kText("请输入邮箱"))
        array.append(itemFir)
        let itemSec = CYTableviewItem(type: .cyItemTimer, name: kText("邮箱验证码"), placeHolder: kText("请输入邮箱验证码"))
        array.append(itemSec)
        
        
        var itemThird:CYTableviewItem
        if AppSecurityInfo.default.model.isOpenGoogle {
            itemThird = CYTableviewItem(type: .cyItemNumber, name: kText("谷歌验证码"), placeHolder: kText("请输入谷歌验证码"))
        }else {
            itemThird = CYTableviewItem(type: .cyItemTimer, name: kText("手机验证码"), placeHolder: kText("请输入短信验证码"))
        }
        array.append(itemThird)
        cellfactory.iteamArray.append(array)
    }
    
}
extension BindEmailViewModel:RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? MobileCodeRequest {
            pramas.updateValue(6, forKey: "codeType")
            pramas.updateValue(model?.areaCode.ch_length != 0 ? model?.areaCode as Any : "" , forKey: "areaCode")
            pramas.updateValue(model?.mobile.ch_length != 0 ? model?.mobile as Any : "", forKey: "mobile")
        }else if let _ = request as? BindEmailRequest {
            pramas.updateValue(6, forKey: "codeType")
            pramas.updateValue(model?.mobile.ch_length != 0 ? model?.mobile as Any : "", forKey: "mobile")
            pramas.updateValue(codeText, forKey: "emailCode")
            pramas.updateValue(emailText, forKey: "email")
            if (model?.isOpenGoogle)! {
                pramas.updateValue(googleText, forKey: "googleCode")
                pramas.updateValue(2, forKey: "type")
            }else {
               pramas.updateValue(googleText, forKey: "mobileCode")
                pramas.updateValue(0, forKey: "type")
            }
        }else if let _ = request as? EmailCodeRequest {
            pramas.updateValue(6, forKey: "codeType")
            pramas.updateValue(emailText, forKey: "email")
            
        }
        
        return pramas
    }
}
